﻿using Pay.WxPay;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Net;
using System.Text;
using System.Web;
using System.Web.Mvc;

namespace SingleUserMall.Web.Controllers
{
    public class UtilController : Controller
    {
        // GET: Util
        public ActionResult Index(string url)
        {
            var _url = HttpUtility.UrlDecode(url);
            var res = GetRequest(_url);
            return Content(res);
        }

        /// <summary>
        /// 根据URL获取网页内容(GET 默认的编码格式)
        /// </summary>
        /// <param name="url">url</param>        
        /// <returns></returns>
        private string GetRequest(string url)
        {
            string result = string.Empty;
            Stream dataStream = null; WebRequest response = null; StreamReader reader = null;
            response = WebRequest.Create(url);
            response.Method = "GET";
            WebResponse wrse = response.GetResponse();
            dataStream = wrse.GetResponseStream();
            reader = new StreamReader(dataStream, Encoding.UTF8);
            result = reader.ReadToEnd();
            dataStream.Close();
            reader.Close();
            return result;
        }

        public ActionResult Redirect(string url)
        {
            url = url.Replace("{appid}", WxPayContext.H5Instance.GetAppId());
            url = url.Replace("{appsecret}", WxPayContext.H5Instance.GetAppSecret());
            return base.Redirect(url);

        }

        public ActionResult Request(string url)
        {
            url = HttpUtility.UrlDecode(url);
            url = url.Replace("{appid}", WxPayContext.H5Instance.GetAppId());
            url = url.Replace("{appsecret}", WxPayContext.H5Instance.GetAppSecret());
            var res = GetRequest(url);
            return Content(res);
        }
    }
}